import java.util.Arrays;
import java.util.EmptyStackException;
import java.util.Stack;
/**
 * @Author: Lenovo
 * @Date: 2024/7/17 20:08
 * @description:
 */
public class MyStack {
    public int[] elem;
    public int useSize;
    public MyStack(){
        this.elem = new int[10];
    }
    public void push(int data){
        if (isFull()){
            this.elem = Arrays.copyOf(elem,2 * elem.length);
        }
        elem[useSize++] = data;
    }
    public boolean isFull(){
        return useSize == this.elem.length;
    }
    public int pop(){
        if (isEmpty()){
            throw new EmptyStackException();
        }
        int val = elem[useSize-1];
        useSize--;
        return val;
    }
    public int peek(){
        if (isEmpty()){
            throw new EmptyStackException();
        }
        return elem[useSize - 1];
    }
    public boolean isEmpty(){
        return useSize == 0;
    }
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack();
        for (int i=0;i<s.length();i++){
            char ch = s.charAt(i);
            if (ch == '('||ch == '{'||ch == '['){
                stack.push(ch);
            }else{
                if (stack.isEmpty()){
                    return false;
                }
                char chr = stack.peek();
                if ((chr=='['&&ch ==']')||(chr == '('&& ch == ')')||(chr == '{')&&(ch == '}')){
                    stack.pop();
                }else{
                    return false;
                }
            }
        }
        if(!stack.isEmpty())  {
            return false;
        }
        return true;
    }
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack();
        for (String str : tokens) {
            if (!operation(str)) {
                int x = Integer.parseInt(str);
                stack.push(x);
            } else {
                // 要注意 val1 和 val2 的位置
                int val2 = stack.pop();
                int val1 = stack.pop();
                switch (str) {
                    case "+":
                        stack.push(val1 + val2);
                        break;
                    case "-":
                        stack.push(val1 - val2);
                        break;
                    case "*":
                        stack.push(val1 * val2);
                        break;
                    case "/":
                        stack.push(val1 / val2);
                        break;
                }
            }
        }
        return stack.pop();
    }
    public boolean operation(String str) {
        if (str.equals("+") || str.equals("-")|| str.equals("*") || str.equals("/")) {
            return true;
        }
        return false;
    }
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        Stack<Integer> stack = new Stack();
        int i = 0;
        int j = 0;
        while(i < pushV.length){
            stack.push(pushV[i]);
            while(j < popV.length&&!stack.empty()&&stack.peek() == popV[j]){
                stack.pop();
                j++;
            }
            i++;
        }
        return stack.empty();
    }
}
